package com.dscyy.readutil;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

class CtProduct{
	private String boxname;
	private String productname;
	private String inputtime;
	private Integer position;
	public String getBoxname() {
		return boxname;
	}
	public void setBoxname(String boxname) {
		this.boxname = boxname;
	}
	public String getProductname() {
		return productname;
	}
	public void setProductname(String productname) {
		this.productname = productname;
	}
	public String getInputtime() {
		return inputtime;
	}
	public void setInputtime(String inputtime) {
		this.inputtime = inputtime;
	}
	public Integer getPosition() {
		return position;
	}
	public void setPosition(Integer position) {
		this.position = position;
	}
	@Override
	public String toString() {
		return "CtProduct [boxname=" + boxname + ", productname=" + productname + ", inputtime=" + inputtime
				+ ", position=" + position + "]";
	}
}


public class TestListSort {
	public static void main(String[] args) {
		List<CtProduct> cps = new ArrayList<CtProduct>();
		for(int i=1;i<=15;i++) {
			CtProduct cp = new CtProduct();
			cp.setBoxname("TestBox");
			cp.setProductname("PRODUCT " + i);
			cp.setPosition(i);
			cp.setInputtime(randomInputtime());
			cps.add(cp);
		}
		System.out.println("==============排序前：=====================");
		for(CtProduct cp: cps) {
			System.out.println(cp);
		}
		System.out.println("==============排序=====================");
		Collections.sort(cps, new Comparator<CtProduct>(){
            public int compare(CtProduct p1, CtProduct p2) {
                //按照Person的年龄进行升序排列
            	int result = p1.getInputtime().compareTo(p2.getInputtime());
                if(result >0 ){
                    return -1;
                }
                if(result==0){
                    return 0;
                }
                return 1;
            }
        });
		System.out.println("==============排序后：=====================");
		for(CtProduct cp: cps) {
			System.out.println(cp);
		}
		
	}

	private static String randomInputtime() {
		return "2018-"+randomNum(12)+"-"+randomNum(28)+" "+randomNum(24)+":"+randomNum(60)+":01";
	}

	private static String randomNum(int i) {
		String num =  (int)Math.floor(Math.random()*i+1)+"";
		return num.length()==1?"0"+num:num;
	}
}
 